package com.dcivision.lucene;

import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.util.HashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.impl.LogFactoryImpl;

import com.dcivision.dms.extractor.AbstractTextExtractor;
import com.dcivision.dms.extractor.ExcelTextExtractor;
import com.dcivision.dms.extractor.HtmlTextExtractor;
import com.dcivision.dms.extractor.PdfTextExtractor;
import com.dcivision.dms.extractor.PowerPointTextExtractor;
import com.dcivision.dms.extractor.RtfTextExtractor;
import com.dcivision.dms.extractor.StandardTextExtractor;
import com.dcivision.dms.extractor.WordTextExtractor;
import com.dcivision.framework.ApplicationException;

public class IndexConverter {
  private static Log log = new LogFactoryImpl().getInstance(IndexConverter.class);
  
  private static HashMap hashForbidden = new HashMap();

  private static HashMap hashExtractor = new HashMap();

  static {
    // set forbidden index converter file extension
    hashForbidden.put("GIF", null);
    hashForbidden.put("JPG", null);
    hashForbidden.put("RAR", null);
    hashForbidden.put("MP3", null);
    hashForbidden.put("CHM", null);
    hashForbidden.put("JSP", null);
    hashForbidden.put("ZIP", null);
    //........
    
    // set Text Extractor
    hashExtractor.put("PPT", PowerPointTextExtractor.class);
    hashExtractor.put("XLS", ExcelTextExtractor.class);
    hashExtractor.put("HTM", HtmlTextExtractor.class);
    hashExtractor.put("HTML", HtmlTextExtractor.class);
    hashExtractor.put("PDF", PdfTextExtractor.class);
    hashExtractor.put("RTF", RtfTextExtractor.class);
    hashExtractor.put("DOC", WordTextExtractor.class);
    // hashExtractor.put("DOC", ASPWordTextExtractor.class);  // Temporary purpose for trail by ASP extraction
    hashExtractor.put("TXT", StandardTextExtractor.class);
    hashExtractor.put("SQL", StandardTextExtractor.class);
    hashExtractor.put("LOG", StandardTextExtractor.class);
    hashExtractor.put("PHP", StandardTextExtractor.class);
    hashExtractor.put("JSP", StandardTextExtractor.class);
    hashExtractor.put("ASP", StandardTextExtractor.class);
    hashExtractor.put("CSV", StandardTextExtractor.class);
    hashExtractor.put("XML", StandardTextExtractor.class);
    hashExtractor.put("VCF", StandardTextExtractor.class);
    hashExtractor.put("VCS", StandardTextExtractor.class);
    hashExtractor.put("CONFIG", StandardTextExtractor.class);
    hashExtractor.put("CONF", StandardTextExtractor.class);
    hashExtractor.put("PROPERTIES", StandardTextExtractor.class);
    hashExtractor.put("JAVA", StandardTextExtractor.class);
    hashExtractor.put("H", StandardTextExtractor.class);
    hashExtractor.put("CC", StandardTextExtractor.class);
    hashExtractor.put("CPP", StandardTextExtractor.class);
    hashExtractor.put("INI", StandardTextExtractor.class);
    hashExtractor.put("JSP", StandardTextExtractor.class);
    
  }

  protected IndexConverter() {
    super();
  }
  
  /**
   * 
   * @param extension
   * @return
   * @throws Exception
   */
  public static boolean isForbidden(String extension) throws Exception {
    return hashForbidden.containsKey(extension);
  }
  
  public static boolean isAllowConvert(String extension) throws Exception {
    return hashExtractor.get(extension)!=null;
  }
  
  /**
   * Get data string from input stream.
   * @param ins
   * @param extension
   * @return
   * @throws Exception
   */
  public static String getText(InputStream ins, String extension) throws Exception {
    String text = null;
    try {
      Class clsExtractor = (Class)hashExtractor.get(extension);
      if (clsExtractor!=null) {
        String encoding = com.dcivision.framework.SystemParameterFactory.getSystemParameter(com.dcivision.framework.SystemParameterConstant.ENCODING);
        if (encoding==null) {
          encoding = "UTF-8";
        }
        Constructor constructor = clsExtractor.getConstructor(new Class[]{java.io.InputStream.class, java.lang.String.class});
        AbstractTextExtractor extractor = (AbstractTextExtractor)constructor.newInstance(new Object[]{ins, encoding});
        text = extractor.getText();
      }
    }catch (Exception ex) {
      log.error(ex, ex);
      throw new ApplicationException(ex);
    }
    return text;
  }
}
